<?php

namespace App\Http\Controllers\Goods;

use App\Http\Requests\ProductRequest;
use App\Models\Attribute;
use App\Models\AttributeGoods;
use App\Models\Goods;
use App\Models\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ProductController extends Controller
{
    
    public function index(Request $request)
    {
//        dd($request->toArray());
        $id=$request->query('goods');
//        dd($id);
        $goods=Goods::where('id',$id)->first();
//        dd($goods->toArray());
       $products=Product::where('goods_id',$id)->get();
            return view('goods.product.index',compact('products','goods'));
    }
    public function create(Request $request)
    {
        $id=$request->query('goods');
        $goods=Goods::where('id',$id)->first();
        $attrId=AttributeGoods::where('goods_id',$goods->id)->get();
        $attributes=[];
        foreach ($attrId as $value){
            $attributes[]=Attribute::where('id',$value->attribute_id)->first();
        }
        return view('goods.product.create',compact('goods','attributes'));
        
    }

    
    public function store(Request $request)
    {
        $request['goods_id']=$request->query('goods');
        $product=Product::create($request->all());
        return back();
//       return redirect()->route('goods.product.index',['goods'=>$request->query('goods')])->with('success','添加货品成功');
    }

    
    public function show(Product $product)
    {
    }

   
    public function edit(Product $product,Request $request)
    {
        $id=$request->query('goods');
        $goods=Goods::where('id',$id)->first();
        //
        $attrId=AttributeGoods::where('goods_id',$goods->id)->get();
        $attributes=[];
        foreach ($attrId as $value){
//            dump($value->toArray());
            $attributes[]=Attribute::where('id',$value->attribute_id)->first();
        }
        //获得这个货品的属性id组合
        $attrIdArr=$product->attributeArr;
//        dd($attrIdArr);
        return view('goods.product.edit',compact('product','goods','attributes','attrIdArr'));
    }

    
    public function update(Request $request, Product $product)
    {
        $request['goods_id']=$request->query('goods');
       $product->update($request->all());
       return redirect()->route('goods.product.index',['goods'=>$request->query('goods')])->with('success','编辑货品成功');
       
    }

    public function destroy(Product $product,Request $request)
    {
//       dd($product->toArray());
        $product->delete();
        return redirect()->route('goods.product.index',['goods'=>$request->query('goods')])->with('success','删除成功');
    }
    //根据属性组合和商品id获得货品数据和库存
    public function getProduct($attrs,$id){
    //$attrs属性组合,$id是商品id
//        根据商品id找到对应的货品
        $arr=explode(',',$attrs);
        $json=json_encode($arr);
        $product=Product::where('attributeArr',$json)->where('goods_id',$id)->first();
     return $product;
//    return ['date'=>$product,'valid'=>1];
    }
    
}
